#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#define SIZE 1024

void write_file(int sockfd)
{
	int n;
	FILE *fp;
	char *filename = "recv.txt";
	char buffer[SIZE];

	fp = fopen(filename, "w");
	while (1)
	{
		n = recv(sockfd, buffer, SIZE, 0);
		if (n <= 0)
		{
			break;
			return;
		}
		fprintf(fp, "%s", buffer);
		bzero(buffer, SIZE);
	}
	return;
}

int main()
{
	char *ip = "127.0.0.1"; // 服务器监听的IP地址，127.0.0.1 是 localhost ，永远代表是本机地址
	int port = 8080;
	int e;

	int sockfd, new_sock;	// sockfd 是向操作系统申请的一个socket资源编号，或者叫做句柄
	struct sockaddr_in server_addr, new_addr;	// 这个是socket连接的参数结构体，
	socklen_t addr_size;
	char buffer[SIZE];

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
	{
		perror("[-]Error in socket");
		exit(1);
	}
	printf("[+]Server socket created successfully.\n");

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = port;
	server_addr.sin_addr.s_addr = inet_addr(ip);

	e = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
	if (e < 0)
	{
		perror("[-]Error in bind");
		exit(1);
	}
	printf("[+]Binding successfull.\n");

	if (listen(sockfd, 10) == 0)
	{
		printf("[+]Listening....\n");
	}
	else
	{
		perror("[-]Error in listening");
		exit(1);
	}

	addr_size = sizeof(new_addr);
	new_sock = accept(sockfd, (struct sockaddr *)&new_addr, &addr_size);
	write_file(new_sock);
	printf("[+]Data written in the file successfully.\n");

	return 0;
}